package com.example.list;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.graphics.Color;
import android.net.Uri;
import android.os.Bundle;
import android.text.Spannable;
import android.text.SpannableStringBuilder;
import android.text.TextPaint;
import android.text.method.LinkMovementMethod;
import android.text.style.ClickableSpan;
import android.text.style.ForegroundColorSpan;
import android.text.style.UnderlineSpan;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.TextView;
import android.widget.Toast;

public class SpannableStringBuilderActivity extends AppCompatActivity {
    private TextView tvSpannableStringBuilder;
    private TextView tvSpannableStringBuilder2;
    private TextView tvSpannableStringBuilder3;
    private String tip = "如超过三分钟仍未收到验证码，请拨打客服热线";
    private String phone = "23689";
    private CheckBox checkbox;

    private Button mlBtn;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_spannable_string_builder);

        tvSpannableStringBuilder = (TextView) findViewById(R.id.tv_SpannableStringBuilder);
        tvSpannableStringBuilder2 = (TextView) findViewById(R.id.tv_SpannableStringBuilder2);
        tvSpannableStringBuilder3 = (TextView) findViewById(R.id.tv_SpannableStringBuilder3);


        mlBtn = (Button) findViewById(R.id.ml_btn);
        mlBtn.setBackground(getResources().getDrawable(R.drawable.shape_red_white_20));
        checkbox = (CheckBox) findViewById(R.id.checkbox);

        checkbox.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (checkbox.isChecked()) {
                    checkbox.setChecked(true);
                    mlBtn.setBackground(getResources().getDrawable(R.drawable.shape_red_20));
                    mlBtn.setClickable(true);
                    checkbox.setBackgroundResource(R.drawable.checkbox_choosed_dianjin);
                } else {
                    checkbox.setChecked(false);
                    mlBtn.setBackground(getResources().getDrawable(R.drawable.shape_red_white_20));
                    mlBtn.setClickable(false);
                    checkbox.setBackgroundResource(R.drawable.checkbox_unchoosed_dianjin);
                }
            }
        });


        example1();
        example2();
        example3();
        setBottomText();
    }
    private void setBottomText() {

        String content = "尚未开通账户？立即开户";

        tvSpannableStringBuilder3.setText(updateTextStyle4(content));

        //添加这句话，否则点击不生效
        tvSpannableStringBuilder3.setMovementMethod(LinkMovementMethod.getInstance());

    }
    private SpannableStringBuilder updateTextStyle4(String content) {
        SpannableStringBuilder spannableString = new SpannableStringBuilder();
        spannableString.append(content);

        //使用UnderlineSpan很好的兼容这个问题
        UnderlineSpan colorSpan1 = new UnderlineSpan() {
            @Override
            public void updateDrawState(TextPaint ds) {
                ds.setColor(Color.parseColor("#0084FF"));//设置颜色
                //   ds.setUnderlineText(false); //去掉下划线
            }
        };

//        注后面的冒号是中文的，后面两个是英文的，
        int phoneBeginIndex = content.indexOf("？")+1;
        int phoneEndIndex = content.indexOf("？") + 5;



        ClickableSpan phoneClickableSpan = new ClickableSpan() {

            @Override
            public void onClick(@NonNull View widget) {
                Toast.makeText(getApplicationContext(), "立即开户", Toast.LENGTH_LONG).show();


            }
        };

        ClickableSpan kefuClickableSpan = new ClickableSpan() {

            @Override
            public void onClick(@NonNull View widget) {
                Toast.makeText(getApplicationContext(), "456", Toast.LENGTH_LONG).show();

            }
        };


        spannableString.setSpan(phoneClickableSpan, phoneBeginIndex, phoneEndIndex, Spannable.SPAN_EXCLUSIVE_INCLUSIVE);

        spannableString.setSpan(colorSpan1, phoneBeginIndex, phoneEndIndex, Spannable.SPAN_EXCLUSIVE_INCLUSIVE);


        return spannableString;
    }

    private void example1() {
        String middle = "noodle";
        String call = "3625";
        String strTip = tip + phone;
        SpannableStringBuilder style = new SpannableStringBuilder(strTip);
        int index = strTip.indexOf(phone);
        style.setSpan(new ForegroundColorSpan(Color.BLUE), index, index + phone.length(), Spannable.SPAN_EXCLUSIVE_INCLUSIVE);
        style.setSpan(new ClickableSpan() {
            @Override
            public void onClick(View view) {
                call();
            }

            @Override
            public void updateDrawState(TextPaint ds) {
                ds.setUnderlineText(false);
            }
        }, index, index + phone.length(), Spannable.SPAN_EXCLUSIVE_INCLUSIVE);


        String strTip2 = tip + phone + middle + call;
        SpannableStringBuilder style2 = new SpannableStringBuilder(strTip2);

        int index2 = strTip2.indexOf(call);
        style2.setSpan(new ForegroundColorSpan(Color.BLUE), index2, index2 + call.length(), Spannable.SPAN_EXCLUSIVE_INCLUSIVE);
        style2.setSpan(new ClickableSpan() {
            @Override
            public void onClick(View view) {
                call();
            }

            @Override
            public void updateDrawState(TextPaint ds) {
                ds.setUnderlineText(false);
            }
        }, index2, index2 + call.length(), Spannable.SPAN_EXCLUSIVE_INCLUSIVE);

        tvSpannableStringBuilder.setText(style);
//        tvSpannableStringBuilder.setText(style2);

        tvSpannableStringBuilder.setMovementMethod(LinkMovementMethod.getInstance());//触发点击事件
    }

    private void example2() {
        //《隐私政策》、《使用条款》书名号及其中的内容需要设置变色和设置可点击事件
        String content = "为了让您更好的使用本软件，请充分阅读并理解《隐私政策》与《使用条款》。";
        tvSpannableStringBuilder2.setText(updateTextStyle2(content));

        //添加这句话，否则点击不生效
        tvSpannableStringBuilder2.setMovementMethod(LinkMovementMethod.getInstance());

    }

    private void example3() {
        //《隐私政策》、《使用条款》书名号及其中的内容需要设置变色和设置可点击事件
        String content = "注：点击“获取验证码”后若长时间未收到验证码短信,请联系客服电话:95578或者点击咨询:在线客服";
//注后面的冒号是中文的，后面两个是英文的，

        tvSpannableStringBuilder3.setText(updateTextStyle(content));

        //添加这句话，否则点击不生效
        tvSpannableStringBuilder3.setMovementMethod(LinkMovementMethod.getInstance());

    }

    private void call() {
        Intent intent = new Intent(Intent.ACTION_DIAL, Uri.parse("tel:" + phone));
        startActivity(intent);
    }

    private SpannableStringBuilder updateTextStyle(String content) {
        SpannableStringBuilder spannableString = new SpannableStringBuilder();
        spannableString.append(content);

        //使用ForegroundColorSpan添加点击事件会出现冲突
        UnderlineSpan colorSpan = new UnderlineSpan() {
            @Override
            public void updateDrawState(TextPaint ds) {
                ds.setColor(Color.parseColor("#0084FF"));//设置颜色
            }
        };


        //使用UnderlineSpan很好的兼容这个问题
        UnderlineSpan colorSpan1 = new UnderlineSpan() {
            @Override
            public void updateDrawState(TextPaint ds) {
                ds.setColor(Color.parseColor("#0084FF"));//设置颜色
                //   ds.setUnderlineText(false); //去掉下划线
            }
        };
//        int privacyBeginIndex = content.indexOf("《");
//        int privacyEndIndex = content.indexOf("》") + 1;
//        int protocolBeginIndex = content.lastIndexOf("《");
//        int protocolEndIndex = content.lastIndexOf("》") + 1;

//        注后面的冒号是中文的，后面两个是英文的，
        int privacyBeginIndex = content.indexOf(":") + 1;
        int privacyEndIndex = content.indexOf(":") + 6;
        int protocolBeginIndex = content.lastIndexOf(":") + 1;
        int protocolEndIndex = content.lastIndexOf(":") + 5;


        ClickableSpan privacyClickableSpan = new ClickableSpan() {

            @Override
            public void onClick(@NonNull View widget) {
                Toast.makeText(getApplicationContext(), "123", Toast.LENGTH_LONG).show();

            }
        };

        ClickableSpan protocolClickableSpan = new ClickableSpan() {

            @Override
            public void onClick(@NonNull View widget) {
                Toast.makeText(getApplicationContext(), "456", Toast.LENGTH_LONG).show();

            }
        };


        spannableString.setSpan(privacyClickableSpan, privacyBeginIndex, privacyEndIndex, Spannable.SPAN_EXCLUSIVE_INCLUSIVE);
        spannableString.setSpan(protocolClickableSpan, protocolBeginIndex, protocolEndIndex, Spannable.SPAN_EXCLUSIVE_INCLUSIVE);
        //字体颜色一定要放在点击事件后面，不然部分手机不会修改颜色
        spannableString.setSpan(colorSpan, protocolBeginIndex, protocolEndIndex, Spannable.SPAN_EXCLUSIVE_INCLUSIVE);
        spannableString.setSpan(colorSpan1, privacyBeginIndex, privacyEndIndex, Spannable.SPAN_EXCLUSIVE_INCLUSIVE);


        return spannableString;
    }

    private SpannableStringBuilder updateTextStyle2(String content) {
        SpannableStringBuilder spannableString = new SpannableStringBuilder();
        spannableString.append(content);

        //使用ForegroundColorSpan添加点击事件会出现冲突
        UnderlineSpan colorSpan = new UnderlineSpan() {
            @Override
            public void updateDrawState(TextPaint ds) {
                ds.setColor(Color.parseColor("#0084FF"));//设置颜色
            }
        };


        //使用UnderlineSpan很好的兼容这个问题
        UnderlineSpan colorSpan1 = new UnderlineSpan() {
            @Override
            public void updateDrawState(TextPaint ds) {
                ds.setColor(Color.parseColor("#0084FF"));//设置颜色
                //   ds.setUnderlineText(false); //去掉下划线
            }
        };
        int privacyBeginIndex = content.indexOf("《");
        int privacyEndIndex = content.indexOf("》") + 1;
        int protocolBeginIndex = content.lastIndexOf("《");
        int protocolEndIndex = content.lastIndexOf("》") + 1;

////        注后面的冒号是中文的，后面两个是英文的，
//        int privacyBeginIndex = content.indexOf(":")+1;
//        int privacyEndIndex = content.indexOf(":") + 6;
//        int protocolBeginIndex = content.lastIndexOf(":")+1;
//        int protocolEndIndex = content.lastIndexOf(":") + 5;


        ClickableSpan privacyClickableSpan = new ClickableSpan() {

            @Override
            public void onClick(@NonNull View widget) {
                Toast.makeText(getApplicationContext(), "123", Toast.LENGTH_LONG).show();

            }
        };

        ClickableSpan protocolClickableSpan = new ClickableSpan() {

            @Override
            public void onClick(@NonNull View widget) {
                Toast.makeText(getApplicationContext(), "456", Toast.LENGTH_LONG).show();

            }
        };


        spannableString.setSpan(privacyClickableSpan, privacyBeginIndex, privacyEndIndex, Spannable.SPAN_EXCLUSIVE_INCLUSIVE);
        spannableString.setSpan(protocolClickableSpan, protocolBeginIndex, protocolEndIndex, Spannable.SPAN_EXCLUSIVE_INCLUSIVE);
        //字体颜色一定要放在点击事件后面，不然部分手机不会修改颜色
        spannableString.setSpan(colorSpan, protocolBeginIndex, protocolEndIndex, Spannable.SPAN_EXCLUSIVE_INCLUSIVE);
        spannableString.setSpan(colorSpan1, privacyBeginIndex, privacyEndIndex, Spannable.SPAN_EXCLUSIVE_INCLUSIVE);


        return spannableString;
    }

}